
// update header info box  
// time :  cur :  start :  
//  romur
//  run state
//  user  

CInfoView = function () {
	
	
};

CInfoView.prototype = {

		// init ... 
		initialize: function() {
			
			// add handler 
			
			// 	 Login Popup  
		//select all the a tag with name equal to modal
		$('a[name=modal]').click(function(e) {
			//Cancel the link behavior
			e.preventDefault();
			
			$('.modal_window').hide();
	
			//Get the A tag
			var id = $(this).attr('href');
			
	
			if ( id == '#changeuser_dlg') {
				em.loginDlg.makeDlg();
		
			} else if ( id == '#change_key') {
	/*
				// nothing  ...  get key. set key .. 
				$('#id_btn_keyset').click( function() {
					var key = $('#session_key').val();
					console.log( 'set key ');
					console.log( key);
					em.control.setKey( key);
					
					$('#mask').hide();
					$('.modal_window').hide();				
				});
				
				$('#key_box > input:button').click( function() {
					$('#mask').hide();
					$('.modal_window').hide();
				});
				*/
	
	
			} else if ( id == '#try_login') {
				if ( ! em.trylogin ) {
					em.trylogin = new CTryLogin();
				} else {
					em.trylogin.reshow();
				}
			} else if ( id == '#initQst') {
				if ( ! em.initQst ) {
					debug.log(this, 'Create Init Qst');
					em.initQst = new CInitQuest();
				}
				
				// restart 
				em.initQst.initialize();		// get prev stored state
				
				debug.log(this, 'Start Init Qst Mode');
				em.initQst.start();				// go 

				
			}
			
			});
			
			
					
			//  start info view timer 
			var self = this;
			setInterval ( function() { self.timerFn(); }, 1000);

			// start time : reset 
			$('#clock > #start_time').html('Start: ');
		}	,
		
		
		
		// timer job 
		timerFn: function () {
			// show cur time 
			var d = new Date();
			 var time_txt = d.getFullYear() + '-' + (d.getMonth() + 1)  + '-' + d.getDate() 
			 + '  ' + d.getHours() + ':' + d.getMinutes() +':' + d.getSeconds();
			$('#clock > #cur_time').html(time_txt);
			
			// debug.log(this, ' timer. in infoview ');
			
			// update remain  dur time
			if ( this.dur) {
				this.dur.remains--;
				this.showDur(this.dur);
			}
			if ( this.mine_info) {
				this.mine_info.nexttime--;
				this.showMineInfo(this.mine_info);
			}
		} , 

		setUserInfo: function(userinfo) {
			$('#userinfo').html(userinfo.name + ' : ' + userinfo.device);
		},
		
		// att info  
		updateRumor: function(data) {
			// var data_rumor_conscribe = {"code":0,"ret":{"hero":[{a:11, d:18, gid: 9, num: 4, max: 10, w:19}]}};
			// var infostr  = 'r : Q:' + parseInt(data.ret.) + ' K:' + parseInt(data.ret.K) + ' A:' + parseInt(data.ret.A);
			var infostr  = 'Num:' + data.ret.hero.length;
			em.util.print_status(data);
			$.each( data.ret.hero , function(i, hero) { infostr += ' id:' + hero.gid + ' '+ hero.num + '/'+ hero.max;	} );
			
			 $("#rumorret > span:first-child").text(infostr);
			 // $("#rumorret > span:first-child").text('test!!!');
		},

		updateRumorTime: function(lasttime) {
			var nextTime = new Date(lasttime.getTime() + 60*60*1000) ;
			 var next_txt = 'next : ' + nextTime.getHours() + ':' + nextTime.getMinutes();
			 $("#rumor_remiantime >span:last-child").html(next_txt);
			 
			 if ( this.rumer_timer ) {
			 	clearInterval(this.rumer_timer );
			 }
			this.rumer_timer =  setInterval(  function() {
			 	// var newDateObj = new Date(oldDateObj.getTime() + diff*60000); 
			 	// var now = new Date(); now.setMinutes(now.getMinutes() + 30); 
			 	var remainDate = new Date (nextTime - new Date());
			 	console.log(remainDate);
			 	$("#rumor_remiantime > span:first-child").html('  Remain: ' + remainDate.getMinutes() + ':' + remainDate.getSeconds());
			 }, 1000*60); 
		
	},
	
	setAutoHntStart: function() {
		var d = new Date();
		 var time_txt = d.getFullYear() + '-' + (d.getMonth() + 1)  + '-' + d.getDate() 
		 + '  ' + d.getHours() + ':' + d.getMinutes() +':' + d.getSeconds();
		
		$('#start_time').text(time_txt);
	},
	
	showBless: function(city) {
		
		// new 
		if ( this.blessCnt != city[23].length) {
			this.bless = city[23].length;
			
			// show img 	
			$('#bless_box').empty();
			$.each (  city[23] , function(i, tem) {
				if ( tem.itemid == 166 || tem.itemid == 167 ) {
					// $('#left_header').after('#clock')
					$('#bless_box').
						append( $('<img>').attr({src: 'images/chrome.gif', width:'20px', height:'20px'}) ) ;
				}
				
			} );
		}
		
		
	},
	
	connected: function(on) {
		var src = '';
		if ( on) {
			src = 'images/connected.png'
		} else {
			src = 'images/notconnected.png'
		}
		$('#connect_state').html($('<img>').attr({src:src, width:'100%', height:'100%'}));
	},
	
	updateUserinfo: function(type, userdata) {
		var str = "";
		var type_class =  {'background-color':'white'}; ;
		if(CDB.getCurUserOptions().pvp) {
			str += 'PV:';
			type_class = {'background-color':'red'};
		}
		if(CDB.getCurUserOptions().sp30) {
			str += 'SP:';
			type_class = {'background-color':'blue'};
		}
		
		if ( this.set  ) return ;
		this.set = true;
		
		// grade 
		if ( typeof userdata === 'number') {
			// str += this.userdata.nick + ' ';
			// str += ' ' + userdata ;	// grade
			// str += ' m:' + this.userdata.money ;	
		}
 		else {
 			this.userdata = userdata;
			str += userdata.nick + ' ';
			str += ' :' + userdata.level ;
			str += ' m:' +userdata.money ; 		
					$('#mode_indicator').css(type_class).show().text(str);	
 		}		
		
		/*
{"id":100,"nick":"[Hip] james!!","nationid":2,"level":7,"money":81,"guild":"",
 "guildid":0,"gpower":0,"gflag":1,"protection":0,"p_end":1325145921,
 "status":0,"city":[{"id":100,"name":"james!!","landtype":4,"x":48,"y":12}],
 "conq":[0,0,null,5],"gift":[],"
 pvp":0,"s":1002},

		*/

		
		// $('#mode_indicator').css(type_class).show().text(str);
	},
	

// {"code":0,"ret":[91,3600,922421]};	
	updateDur: function(ret_dur) {
		debug.log( this, 'this', this, ret_dur);
		
		
		// TODO  : 이건 controller 에서 관리 하기로.. next time 은 controller 에게 query . . 
		if ( !this.dur ) this.dur = {};
		
		this.dur.code = ret_dur.code;
		this.dur.remains =  ret_dur.ret[1];
		this.dur.remaind =  ret_dur.ret[0];
		//this.dur.endtime = new Date($.now() + this.dur.remains * 1000);
		
		this.showDur(this.dur);
	},
	
	showDur: function(dur) {
		var str = "";
		str += (dur.code == 0 ) ? 'Suc! ' : 'Fail! ';
		str += 'dur:' + (( dur.remaind === undefined) ? 'Non' : dur.remaind );
		str += 'secs:' + (( dur.remains === undefined) ? 'Non' : dur.remains );
		
		$('#dur_result').empty().text(str);
	},
	
	updateMine_PVP: function(mine_info) {
		debug.log( this, 'this', this, mine_info);
		
		mine_info.dur;
		mine_info.nextdate;
		// TODO  : 이건 controller 에서 관리 하기로.. next time 은 controller 에게 query . . 
		if ( !this.mine_info ) this.mine_info = {};
		
		$.extend (this.mine_info, mine_info );
		
		this.showMineInfo(this.mine_info);

	},
	
	showMineInfo: function(mine_info) {
		var str = 'D:' + mine_info.dur 
			+ ' re:' + mine_info.nexttime
			+ ' next:' + CUtil.TimeToString(mine_info.nextdate);
		$("#rumorret > span:first-child").text(str);				
	},
	
	// ret[0]  : 1 : ok , 0 : fail : 
	updateEnhance: function(enhance_ret, tem) {
		var str  = '';
		var str_next = '';
		str += (enhance_ret && enhance_ret.code == 0 && enhance_ret.ret[0] != 0 )  ? 'OK: ' :  'Fail: ';
		
		if (enhance_ret == -1 ) {
			str += 'No tem';
		} else if (enhance_ret == -3  ) {
			str += 'Low Per:' 
		} else if ( enhance_ret &&  enhance_ret.ret ) {
			 
			// when fail : cd has  cooltime 
			// when ok  :  1 ,900 ,40000 ,[0, 0, 260, 3 more...] ,1
			if (  enhance_ret.ret.cd  !== undefined  ) {
				str += 'Cool :' + enhance_ret.ret.cd;
			} 
			str_next = CUtil.TimeToString(enhance_ret.nexttime);
			
		}
		
		
		str += ' ' + CUtil.TimeToString( new Date());
		
// 		  {"id":612505,"sid":20,"attr":[0,0,0,38,0,0],"up":14,"p":900000}
		var up = ( tem) ? CDB.TEM.SIDNAME [tem.sid]  + tem.up + " : " : '' ;

		$('.enhance_result').html(str + "<br/>" + up + str_next);
	},
	
};
